<?php

class Client_NetworksController extends App_Controller_Action
{	
	public function indexAction()
	{
		$request = $this->getRequest();
		$user_id = $this->getUserId();
		$user_role = $this->getUserRole();
		
		if ($user_role != 'manager' && $user_role != 'IT')
		{
			$this->view->error_msg = 'No Priviliege';	
		}
		else 
		{
			//check IP
			$ip=Model_ActionLog::getIp();
			if (($ip!='70.50.227.211')and($ip!='199.21.151.11')and($ip!='127.0.0.1')and(substr($ip,0,10)!='192.168.25')and(substr($ip,0,10)!='192.168.26')){
				$this->view->error_msg = 'IP address not allowed.';				
			}else{
				$model_client = new Model_UserClient();
			
				$remote_call = 'http://admin.clicksor.com/admin/server-rpc.php';
				$client = new Zend_XmlRpc_Client($remote_call);	
	
				$params=array();
				$params['ip']=$ip;  
				$networks = $client->call("YesupAcc.getPartnerNetworks",array($params));
				//Zend_Debug::dump($networks);
				$this->view->networks=$networks;
				$this->view->client_id=$user_id;
				$this->view->user_role=$user_role;
				
			}
		}
		
    }
    
    
	public function toNetworkAction()
	{
		$this->_helper->layout->disableLayout();

		$dpt_id = $this->getRequest()->getParam('nid');
		$client_id = $this->getRequest()->getParam('cid');

		$model_client = new Model_UserClient();
		$login_url = $model_client->setClientSecurityKey($dpt_id, $client_id);
		
		$remote_call = 'http://admin.clicksor.com/admin/server-rpc.php';
		$client = new Zend_XmlRpc_Client($remote_call);	

		$params=array();
		$ip=Model_ActionLog::getIp();
		$params['ip']=$ip;  
		$networks = $client->call("YesupAcc.getPartnerNetworks",array($params));
		foreach ($networks as $info )
		{
			if ($info['networkid'] == $dpt_id) 
			{
				$admin_url = $info['admin_url'];
				break;
			}
		}
		
		if ($dpt_id == 1 || $dpt_id >100 && $dpt_id < 200)
		{
			$admin_url = str_replace('/admin', '', $admin_url);
			$login_url = "http://".$admin_url."/admin/".$login_url;
		} 
		else $login_url = "http://".$admin_url."/".$login_url;
	
		$valid_url = Zend_Uri::check($login_url);
		//Zend_Debug::dump($login_url);exit;
    
		if ($valid_url) $this->_redirect($login_url); 
		else throw new Exception('failed to remote site.');
	
		exit;
	}
    
		
}

